package com.cys.dm.rest.service;

import com.cys.dm.client.scheduler.DmSchedulerSeatClient;
import com.cys.dm.client.scheduler.DmSchedulerSeatPriceClient;
import com.cys.dm.entity.scheduler.DmSchedulerSeat;
import com.cys.dm.exceptions.DmException;
import com.cys.dm.vo.scheduler.DmSchedulerSeatResponse;
import com.cys.dm.vo.scheduler.SchedulerSeatVo;
import com.cys.dm.vo.scheduler.SeatInfoVo;
import com.cys.dm.vo.scheduler.SeatPriceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author blue_t
 * @date 2020-12-23 9:40
 */
@Service
public class DmSchedulerSeatService {
    @Autowired
    private DmSchedulerSeatClient dmSchedulerSeatClient;
    @Autowired
    private DmSchedulerSeatPriceClient dmSchedulerSeatPriceClient;


    /**
     * 根据排期获取剧场作为信息
     * @param map
     * @return
     */
    public DmSchedulerSeatResponse getSchedule(Map<String,Long> map){
        //排期ID
        Long scheduleId =  map.get("scheduleId");
        //剧场ID
        Long cinemaId = map.get("cinemaId");


        //V对象
        DmSchedulerSeatResponse vo = new DmSchedulerSeatResponse();


        //排期ID
        vo.setScheduleId(scheduleId);
        //剧场Id
        vo.setCinemaId(cinemaId);

        //座位价格VO类
        List<SeatPriceVo> seatPriceVoList = new ArrayList<>();
        //座位价格源数据
        dmSchedulerSeatPriceClient.queryItemPrice(scheduleId).forEach(dmSchedulerSeatPrice -> {
            SeatPriceVo seatPriceVo = new SeatPriceVo();
            //座位等级
            switch ((int) dmSchedulerSeatPrice.getAreaLevel()){
                case 1:
                    seatPriceVo.setAreaLevelName("A");
                    break;
                case 2:
                    seatPriceVo.setAreaLevelName("B");
                    break;
                case 3:
                    seatPriceVo.setAreaLevelName("C");
                    break;
                default:
                    break;
            }
            //座位价格
            seatPriceVo.setPrice(dmSchedulerSeatPrice.getPrice());
            seatPriceVoList.add(seatPriceVo);
        });

        //座位价格
        vo.setSeatPriceList(seatPriceVoList);

        //座位信息VO类
        List<SeatInfoVo> seatInfoVoList = new ArrayList<>();
        //座位信息源数据
        dmSchedulerSeatClient.findSchedulerSeatBySchedulerId(scheduleId).forEach(dmSchedulerSeat -> {
            SeatInfoVo seatInfoVo = new SeatInfoVo();
            //座位ID
            seatInfoVo.setId(dmSchedulerSeat.getId()+"");
            //X坐标
            seatInfoVo.setX(dmSchedulerSeat.getX());
            //Y坐标
            seatInfoVo.setY(dmSchedulerSeat.getY());
            //区域级别
            switch ((int)dmSchedulerSeat.getAreaLevel()){
                case 1:
                    seatInfoVo.setAreaLevel("A");
                    break;
                case 2:
                    seatInfoVo.setAreaLevel("B");
                    break;
                case 3:
                    seatInfoVo.setAreaLevel("C");
                    break;
                default:
                    break;
            }
            //剧场Id
            seatInfoVo.setCinemaId(cinemaId.toString());
            //座位排序
            seatInfoVo.setSort(dmSchedulerSeat.getSort()+"");
            //座位状态
            seatInfoVo.setStatus(dmSchedulerSeat.getStatus());
            seatInfoVoList.add(seatInfoVo);
        });

        //座位信息
        vo.setSeatInfoList(seatInfoVoList);

        return vo;
    };

    /**
     * 根据剧场Id查询剧场原始座位
     * @param map 数据
     * @return
     */
    public SchedulerSeatVo getSeatList(Map<String,Long> map){
        //排期ID
        Long scheduleId = map.get("scheduleId");
        //剧场ID
        Long cinemaId = map.get("cinemaId");

        if (scheduleId == null || cinemaId == null){
            throw new DmException("2001","参数异常");
        }

        //获取X轴
        Integer x = dmSchedulerSeatClient.findScheduleSeatByIDAndX(scheduleId);
        //获取y轴
        Integer y = dmSchedulerSeatClient.findScheduleSeatByIDAndY(scheduleId);


        //VO数据
        SchedulerSeatVo vo = new SchedulerSeatVo();
        String[] seatList = new String[x];

        //循环获取赋值
        for (int i = 1; i <= x; i++) {
            StringBuffer str = new StringBuffer();
            for (int j = 1; j <= y; j++) {
                DmSchedulerSeat seat = dmSchedulerSeatClient.findSchedulerSeatByIdAndXAndY(scheduleId, i, j);
                switch ((int)seat.getAreaLevel()){
                    case 1:
                        str.append("a");
                        break;
                    case 2:
                        str.append("b");
                        break;
                    case 3:
                        str.append("c");
                        break;
                    default:
                        str.append("_");
                        break;
                }
            }
            seatList[i-1] = str.toString();

        }

        vo.setSeatArray(seatList);

        return vo;
    }

}
